package com.pw.study.flink.sql;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author: linux_future
 * @since: 2022/3/11
 **/
public class $07SqlKafka {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        readKafka(tEnv);
        writeKafka(tEnv);

    }

    private static void writeKafka(StreamTableEnvironment tEnv) {
        tEnv
                .executeSql("create table abc(" +
                        " id string, " +
                        " ts bigint, " +
                        " vc int" +
                        ")with(" +
                        " 'connector' = 'kafka', " +
                        "  'topic' = 's2'," +
                        "  'properties.bootstrap.servers' = 'hadoop162:9092',\n" +
                        "  'format' = 'json', " +
                        "  'sink.partitioner' = 'round-robin' " +
                        ")");


        tEnv.sqlQuery("select * from tb_kafka").executeInsert("abc");

    }

    private static void readKafka(StreamTableEnvironment tEnv) {
        tEnv
                .executeSql("create table tb_kafka(" +
                        " id string, " +
                        " ts bigint, " +
                        " vc int" +
                        ")with(" +
                        " 'connector' = 'kafka', " +
                        "  'topic' = 'sql'," +
                        "  'properties.bootstrap.servers' = 'hadoop162:9092',\n" +
                        "  'properties.group.id' = 'atguigu',\n" +
                        "  'scan.startup.mode' = 'latest-offset',\n" +
                        "  'format' = 'json'" +
                        ")");

       // tEnv.sqlQuery("select * from tb_kafka ").execute().print();

    }

}
